﻿namespace HuaWei.CommClass
{
    using DevExpress.XtraEditors;
    using DevExpress.XtraEditors.Controls;
    using DevExpress.XtraEditors.Repository;
    using DevExpress.XtraGrid;
    using DevExpress.XtraGrid.Columns;
    using DevExpress.XtraGrid.Views.Card;
    using DevExpress.XtraGrid.Views.Grid;
    using DevExpress.XtraTab;
    using DevExpress.XtraTreeList.Nodes;
    using System;
    using System.Data;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;

    public static class DevCCHelp
    {
        public static void CloseTabControl(XtraTabControl tabControl)
        {
            for (int i = 0; i < tabControl.TabPages.Count; i++)
            {
                XtraTabPage page = tabControl.TabPages[i];
                if (page.Controls.Count > 0)
                {
                    Control control = page.Controls[0];
                    if (control is Form)
                    {
                        ((Form) control).Close();
                    }
                }
            }
        }

        public static void ControlColumn(GridColumnCollection columns, bool isReadOnly, int controlLeve)
        {
            foreach (GridColumn column in columns)
            {
                switch (controlLeve)
                {
                    case 1:
                    {
                        if ((column.Tag != null) && !column.Tag.Equals(""))
                        {
                            break;
                        }
                        column.OptionsColumn.ReadOnly = isReadOnly;
                        continue;
                    }
                    case 2:
                    {
                        if ((column.Tag != null) && !column.Tag.Equals(""))
                        {
                            goto Label_0112;
                        }
                        column.OptionsColumn.ReadOnly = isReadOnly;
                        continue;
                    }
                    default:
                    {
                        continue;
                    }
                }
                if (CHelp.ConvertInt(column.Tag, 0).Equals(-2))
                {
                    column.OptionsColumn.ReadOnly = false;
                }
                else if (CHelp.ConvertInt(column.Tag, 0).Equals(-1))
                {
                    column.OptionsColumn.ReadOnly = false;
                }
                else if (!CHelp.ConvertInt(column.Tag, 0).Equals(-3))
                {
                    column.OptionsColumn.ReadOnly = true;
                }
                continue;
            Label_0112:
                if (CHelp.ConvertInt(column.Tag, 0).Equals(-1))
                {
                    column.OptionsColumn.ReadOnly = isReadOnly;
                }
                else if (CHelp.ConvertInt(column.Tag, 0).Equals(-2))
                {
                    column.OptionsColumn.ReadOnly = false;
                }
                else if (!CHelp.ConvertInt(column.Tag, 0).Equals(-3))
                {
                    column.OptionsColumn.ReadOnly = true;
                }
            }
        }

        public static void ControlForm(Control.ControlCollection cc, bool isReadOnly, int controlLeve)
        {
            foreach (Control control in cc)
            {
                switch (controlLeve)
                {
                    case 1:
                    {
                        if ((control.Tag != null) && !control.Tag.Equals(""))
                        {
                            break;
                        }
                        smethod(control, isReadOnly, controlLeve);
                        continue;
                    }
                    case 2:
                    {
                        if ((control.Tag != null) && !control.Tag.Equals(""))
                        {
                            goto Label_00FE;
                        }
                        smethod(control, isReadOnly, controlLeve);
                        continue;
                    }
                    default:
                    {
                        continue;
                    }
                }
                if (CHelp.ConvertInt(control.Tag, 0).Equals(-2))
                {
                    smethod(control, false, controlLeve);
                }
                else if (CHelp.ConvertInt(control.Tag, 0).Equals(-1))
                {
                    smethod(control, false, controlLeve);
                }
                else if (!CHelp.ConvertInt(control.Tag, 0).Equals(-3))
                {
                    smethod(control, isReadOnly, controlLeve);
                }
                continue;
            Label_00FE:
                if (CHelp.ConvertInt(control.Tag, 0).Equals(-1))
                {
                    smethod(control, isReadOnly, controlLeve);
                }
                else if (CHelp.ConvertInt(control.Tag, 0).Equals(-2))
                {
                    smethod(control, false, controlLeve);
                }
                else if (!CHelp.ConvertInt(control.Tag, 0).Equals(-3))
                {
                    smethod(control, isReadOnly, controlLeve);
                }
            }
        }

        public static string GetComboList(DataTable dtList)
        {
            if (((dtList != null) && (dtList.Rows.Count > 0)) && (dtList.Columns.Count == 1))
            {
                string str = string.Empty;
                for (int i = 0; i < dtList.Rows.Count; i++)
                {
                    str = str + dtList.Rows[i][0].ToString() + Convert.ToChar(Keys.Tab);
                }
                if (str.Length > 1)
                {
                    str = str.Remove(str.Length - 1);
                }
                return str;
            }
            return "";
        }

        public static void SetCheckedChildNodes(TreeListNode node, CheckState check)
        {
            for (int i = 0; i < node.Nodes.Count; i++)
            {
                node.Nodes[i].CheckState = check;
                SetCheckedChildNodes(node.Nodes[i], check);
            }
        }

        public static void SetCheckedParentNodes(TreeListNode node, CheckState check)
        {
            if (node.ParentNode != null)
            {
                bool flag = false;
                for (int i = 0; i < node.ParentNode.Nodes.Count; i++)
                {
                    CheckState checkState = node.ParentNode.Nodes[i].CheckState;
                    if (!check.Equals(checkState))
                    {
                        flag = !flag;
                        break;
                    }
                }
                node.ParentNode.CheckState = flag ? CheckState.Indeterminate : check;
                SetCheckedParentNodes(node.ParentNode, check);
            }
        }

        public static void SetComboList(ComboBoxEdit ComboList, DataTable dtList, [Optional, DefaultParameterValue("")] string FirstRowText, [Optional, DefaultParameterValue(false)] bool bSelectFirstItem)
        {
            int num = (dtList != null) ? dtList.Rows.Count : 0;
            ComboList.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
            ComboList.Properties.Items.Clear();
            if (!string.IsNullOrEmpty(FirstRowText))
            {
                ComboList.Properties.Items.Add(FirstRowText);
            }
            if (num > 0)
            {
                for (int i = 0; i < num; i++)
                {
                    ComboList.Properties.Items.Add(dtList.Rows[i][0].ToString());
                }
            }
            if (bSelectFirstItem)
            {
                ComboList.SelectedIndex = 0;
            }
        }

        public static void SetGridButton(GridControl grid)
        {
            if (grid.UseEmbeddedNavigator)
            {
                grid.EmbeddedNavigator.Buttons.CancelEdit.Visible = false;
                grid.EmbeddedNavigator.Buttons.Edit.Visible = false;
                grid.EmbeddedNavigator.Buttons.Append.Visible = false;
                grid.EmbeddedNavigator.Buttons.EndEdit.Visible = false;
                grid.EmbeddedNavigator.Buttons.Remove.Visible = false;
                grid.EmbeddedNavigator.Buttons.Prev.Visible = false;
                grid.EmbeddedNavigator.Buttons.PrevPage.Visible = false;
                grid.EmbeddedNavigator.Buttons.Next.Visible = false;
                grid.EmbeddedNavigator.Buttons.NextPage.Visible = false;
                grid.EmbeddedNavigator.Buttons.First.Visible = false;
                grid.EmbeddedNavigator.Buttons.Last.Visible = false;
                grid.EmbeddedNavigator.TextLocation = NavigatorButtonsTextLocation.None;
            }
        }

        public static string SetLookUpList(RepositoryItemLookUpEdit properties, DataRow[] rows, string colValue, string colName)
        {
            properties.Columns.Clear();
            properties.DataSource = null;
            string str = string.Empty;
            if (rows.Length > 0)
            {
                properties.Columns.Add(new LookUpColumnInfo(colValue));
                properties.Columns[colValue].Visible = false;
                properties.Columns.Add(new LookUpColumnInfo(colName));
                DataTable table = new DataTable();
                table.Columns.Add(colValue, typeof(string));
                table.Columns.Add(colName, typeof(string));
                foreach (DataRow row in rows)
                {
                    DataRow row2 = table.NewRow();
                    row2[colValue] = row[colValue];
                    row2[colName] = row[colName];
                    table.Rows.Add(row2);
                }
                properties.DataSource = table;
                properties.ValueMember = colValue;
                properties.DisplayMember = colName;
                str = rows[0][0].ToString();
            }
            properties.ShowHeader = false;
            properties.ShowFooter = false;
            return str;
        }

        public static string SetLookUpList(RepositoryItemLookUpEdit properties, DataTable dt, [Optional, DefaultParameterValue(0)] int ValueCol, int NameCol, [Optional, DefaultParameterValue(false)] bool bSelectFirstItem, [Optional, DefaultParameterValue("")] string sFirstRowText)
        {
            properties.Columns.Clear();
            properties.DataSource = null;
            string str = string.Empty;
            if (((dt == null) || (dt.Rows.Count <= 0)) || (dt.Columns.Count <= 1))
            {
                return str;
            }
            properties.Columns.Add(new LookUpColumnInfo(dt.Columns[ValueCol].ColumnName.ToString()));
            properties.Columns[ValueCol].Visible = false;
            properties.Columns.Add(new LookUpColumnInfo(dt.Columns[NameCol].ColumnName.ToString()));
            if (!string.IsNullOrEmpty(sFirstRowText))
            {
                DataRow row = dt.NewRow();
                row[dt.Columns[ValueCol]] = -1;
                row[dt.Columns[NameCol]] = sFirstRowText;
                dt.Rows.InsertAt(row, 0);
            }
            properties.DataSource = dt;
            properties.ValueMember = dt.Columns[ValueCol].ColumnName;
            properties.DisplayMember = dt.Columns[NameCol].ColumnName;
            return dt.Rows[0][0].ToString();
        }

        private static void smethod(Control control, bool flag, int num)
        {
            if ((control.Tag == null) || !control.Tag.Equals(-3))
            {
                if (control is BindingNavigator)
                {
                    BindingNavigator bindingNavigator = control as BindingNavigator;
                    smethod1(bindingNavigator, flag, num);
                }
                else if (control is GridControl)
                {
                    GridControl control2 = control as GridControl;
                    if ((control2.Tag != null) && control2.Tag.Equals("1"))
                    {
                        control2.UseEmbeddedNavigator = !flag;
                    }
                    if (control2.FocusedView is GridView)
                    {
                        ((GridView) control2.FocusedView).OptionsCustomization.AllowQuickHideColumns = false;
                        ControlColumn(((GridView) control2.FocusedView).Columns, flag, num);
                    }
                    else if (control2.FocusedView is CardView)
                    {
                        (control2.FocusedView as CardView).OptionsBehavior.ReadOnly = flag;
                    }
                }
                else if (control is TextEdit)
                {
                    ((TextEdit) control).Properties.ReadOnly = flag;
                }
                else if (control is MemoEdit)
                {
                    ((MemoEdit) control).Properties.ReadOnly = flag;
                }
                else if (control is SimpleButton)
                {
                    ((SimpleButton) control).Enabled = !flag;
                }
                else if (control is CheckEdit)
                {
                    ((CheckEdit) control).Enabled = !flag;
                }
                else if (control.Controls.Count > 0)
                {
                    ControlForm(control.Controls, flag, num);
                }
            }
        }

        private static void smethod1(BindingNavigator bindingNavigator, bool flag, int num)
        {
            foreach (ToolStripItem item in bindingNavigator.Items)
            {
                switch (num)
                {
                    case 1:
                    {
                        if ((item.Tag != null) && !item.Tag.Equals(""))
                        {
                            break;
                        }
                        item.Enabled = !flag;
                        continue;
                    }
                    case 2:
                    {
                        if ((item.Tag != null) && !item.Tag.Equals(""))
                        {
                            goto Label_0104;
                        }
                        item.Enabled = !flag;
                        continue;
                    }
                    default:
                    {
                        continue;
                    }
                }
                if (CHelp.ConvertInt(item.Tag, 0).Equals(-2))
                {
                    item.Enabled = true;
                }
                else if (CHelp.ConvertInt(item.Tag, 0).Equals(-1))
                {
                    item.Enabled = true;
                }
                else if (!CHelp.ConvertInt(item.Tag, 0).Equals(-3))
                {
                    item.Enabled = false;
                }
                continue;
            Label_0104:
                if (CHelp.ConvertInt(item.Tag, 0).Equals(-1))
                {
                    item.Enabled = !flag;
                }
                else if (CHelp.ConvertInt(item.Tag, 0).Equals(-2))
                {
                    item.Enabled = true;
                }
                else if (!CHelp.ConvertInt(item.Tag, 0).Equals(-3))
                {
                    item.Enabled = false;
                }
            }
        }
    }
}

